package GUIs;
import java.awt.BorderLayout;
import java.awt.Font;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

import DeportManage.*;
import MVC.*;

public class CustomerPanel extends JPanel implements Observer{
	
		private DataObserver data_observer;
		private JTextArea customer_table;
		/**
		 * Constructor to create an object  of Customer with data_observer and initial a set of valriable
		 * @param data_observer the object of Observer pattern 
		 */
		public CustomerPanel(DataObserver data_observer){
			this.data_observer = data_observer;
			data_observer.registerObserver(this);
			this.setLayout(new BorderLayout(5,5));			
			customer_table = new JTextArea();
			customer_table.setSize(100, 50);
			customer_table.setEditable(false);
			customer_table.setFont(new Font("Arial",Font.LAYOUT_LEFT_TO_RIGHT, 15));
			JScrollPane customer_scollpane = new JScrollPane(customer_table);
			this.add(new JLabel("Customer List :"),BorderLayout.NORTH);
			this.add(customer_scollpane,BorderLayout.CENTER);
			update();
		}
		/**
		 * Method to update and show the list of customers
		 */
		@Override
		public void update() {			
			int index = 0;
			String customer_text = "Seq No.         Name                     Parcel ID      \n";
			customer_text += "-----------------------------------------------------------\n";
			List<Customer>  customer_list = data_observer.getCustList().getCustomerList();
			Iterator<Customer> iterator = customer_list.iterator();
			while(iterator.hasNext()){
				Customer c = iterator.next();
				customer_text += String.format("%-16s", c.getSequenceNo());
				customer_text += String.format("%-25s", c.getName().getFullName());
				customer_text += String.format("%-9s", c.getParcelID());
				customer_text += "\n-----------------------------------------------------------";
				customer_text += "\n";				
				index++;
			}
			this.customer_table.setText(customer_text);
		}
}
